import java.util.Random;
import java.util.Scanner;

public class test {
    public static void main(String[] args) {
//        ShunXv();
//        XuanZe1();
//        JiOu();
//        RunNian();
//        XuanZe2();
//        XunHuan1();
//        XunHuan2();
//        XunHuan3();
//        ShuChu();
//        ShuRu1();
//        ShuRu2();
//        CaiShu();
//        SuShu();
//        ZiMiShu();
//        GeShu();
//        Nine();
        Num();
//        X();
    }
    //顺序
    public static void ShunXv() {
        System.out.println("aaa");
        System.out.println("bbb");
        System.out.println("ccc");
    }
    public static void XuanZe1() {
        int score = 92;
        //if
        if(score >= 90){
            System.out.println("吃个大鸡腿");
        }
        //if...else
        if (score >= 90){
            System.out.println("吃个鸡腿");
        }else {
            System.out.println("继续努力");
        }
        //if...if else...if else...else
        if (score >= 90){
            System.out.println("真厉害");
        } else if (score <= 85 && score >= 60){
            System.out.println("继续加油");
        } else {
            System.out.println("下课来办公室");
        }
        //else会跟最近的if匹配
        //最好加大括号{}
    }
    public static void XuanZe2() {
        //不能做switch参数的数据类型有哪些  基本数据类型当中：boolean float  double  long
        int a = 6;
        switch (a) {
            case 1:
                System.out.println("A");
                break;
            case 2:
                System.out.println("B");
                break;
            case 6:
                System.out.println("F");
                break;
            case 8:
                System.out.println("H");
                break;
            case 10:
                System.out.println("J");
                break;
            default:
                System.out.println("结束");
                break;
        }
        String str = "hello";
        switch (str) {
            case "abc":
                System.out.println("1");
                break;
            case "hello":
                System.out.println("2");
                break;
            default:
                System.out.println("结束");
                break;
        }
    }
    //判断一个数是奇数还是偶数
    public static void JiOu() {
        int num = 10;
        if((num % 2) == 0){
            System.out.println("偶数");
        }else {
            System.out.println("奇数");
        }
    }
    //判断一个年份是否为闰年
    //闰年分为普通闰年（是4的倍数且不是100的倍数）和世纪闰年（400的倍数）
    public static void RunNian() {
        int year = 2024;
        for (year = 1000;year <= 2000;year++){
            if(year % 100 == 0){
                if((year % 400) == 0){
                    System.out.println(year+"是闰年");
                }else {
                    System.out.println(year+"不是闰年");
                }
            } else if (year % 4 == 0) {
                System.out.println(year+"是闰年");
            }else {
                System.out.println(year+"不是闰年");
            }
        }
    }
    //while循环
    public static void XunHuan1() {
        //打印1-10
        int num = 1;
        while (num <= 10) {
            System.out.println(num);
            num++;
        }
        //计算1-100的和
        int num1 = 1;
        int sum = 0;
        while (num1 <= 100) {
            sum += num1;
            num1++;
        }
        System.out.println(sum);
        //计算5的阶乘
        int num2 = 1;
        int ret = 1;//不要赋0
        while(num2 <= 5) {
            ret *= num2;
            num2++;
        }
        System.out.println(ret);
        //1!+2!+3!+4!+5!+6!
        int k = 1;
        int s = 0;
        while (k <= 6) {
            int num3 = 1;
            int ret1 = 1;
            while (num3 <= k) {
                ret1 *= num3;
                num3++;
            }
            System.out.println(ret1);
            k++;
            s += ret1;
        }
        System.out.println(s);

        //break  直接跳出循环
        //输出结果  1
        int n = 1;
        while (n <= 5) {
            if (n == 2) {
                break;
            }
            System.out.println(n);
            n++;
        }
        //找到100 - 200中第一个3的倍数
        //输出结果 102
        int i = 100;
        while (i <= 200) {
            if(i%3 == 0) {
                System.out.println(i);
                break;
            }
            i++;
        }
        //continue  跳出此次循环 执行下次循环
        //输出结果  1 3 4 5
        int m = 1;
        while (m <= 5) {
            if (m == 2) {
                m++;//如果不加这条语句，m会一直为2，程序进入死循环
                continue;
            }
            System.out.println(m);
            m++;
        }
        //求100 - 200 之间既能被3整除又能被4整除的数字  使用continue
        int x = 100;
        while (x <= 200) {
            //条件也可以写成 x%12 != 0
            if(x%3 != 0 || x%4 != 0) {
                x++;
                continue;
            }
            System.out.println(x);
            x++;
        }
    }
    //for循环   fori
    public static void XunHuan2() {
        //从1加到10
        int sum = 0;
        for (int i = 0; i <= 10; i++) {
            sum += i;
        }
        System.out.println(sum);
        //计算1 - 5的阶乘的和
        int sum1 = 0;
        for (int j = 1; j <= 5; j++) {
            int ret1 = 1;
            for (int k = 1; k <= j; k++) {
                ret1 *= k;
                System.out.println(ret1);
            }
            sum1 += ret1;
        }
        System.out.println(sum1);
    }
    //do-while循环  至少会执行一次循环
    public static void XunHuan3() {
        int a = 0;
        do {
            System.out.println(a);
        }while (a != 0);
    }
    //输出
    public static void ShuChu() {
        System.out.println("输出且换行");
        System.out.print("输出不换行");
        System.out.printf("%d\n",10);
    }
    //单个数据输入
    public static void ShuRu1() {
        //输入要导入这个包【include】
        Scanner scan = new Scanner(System.in);
        System.out.println("输入一个年龄：");
        int age = scan.nextInt();//读入一个int类型的整数

        scan.nextLine();//把上一个输入的回车读走

        //nextLine会把上一个输入的回车读入
        System.out.println("请输入你的姓名：");
        String name = scan.nextLine();//读入一行 scan.next()是遇到空格结束输入

        System.out.println("请输入你的工资：");
        float salary = scan.nextFloat();

        System.out.println("姓名："+name);
        System.out.println("年龄"+age);
        System.out.println("工资"+salary);
        //scan.close();//可以认为scan是一个资源，不用了要关闭
        //当包含两个及两个以上的Scanner实例时，
        //其中一个Scanner类的实例执行scanner.close()方法会关闭其他潜在的InputStream流，
        //导致其他Scanner的扫描器无法读取输入流
    }
    //多个数据输入
    public static void ShuRu2() {
        //导包
        Scanner scan = new Scanner(System.in);
        //循环读入多个整数
        while (scan.hasNextInt()) {
            int age = scan.nextInt();
            System.out.println(age);
        }
    }

    //猜数字
    public static void CaiShu() {
        Scanner scan = new Scanner(System.in);
        Random random = new Random();//导包
        int randNum = random.nextInt(101);//左闭右开[0,101)生成0-100的随机数
        //50 - 100的随机数 random.nextInt(50)+50;---[0,50)+50--[50,100)
        System.out.println("请猜一个1-100的数：");
        while (true) {
            System.out.println("请输入你要猜的数字：");
            int num = scan.nextInt();
            if (num < randNum) {
                System.out.println("猜小了");
            } else if (num > randNum) {
                System.out.println("猜大了");
            }else {
                System.out.println("猜对了");
                break;
            }
        }
    }
    //判断一个数是否为素数
    public static void SuShu() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入要判断的数：");
        int num = scan.nextInt();
        int i = 2;
        //java中开平方的方法  Math.sqrt
        for (; i <= Math.sqrt(num); i++) {
            if(num%i == 0) {
                System.out.println(num+"不是素数");
                break;
            }
        }
        //跳出循环有两种情况：1.循环正常结束 2.遇到了break
        if (i > Math.sqrt(num)) {
            System.out.println(num+"是素数");
        }
    }
    //自幂数
    public static void ZiMiShu() {
        for (int i = 1; i <= 999999; i++) {
            //i = 123
            //判断i是几位数
            int ret = i;
            int count = 0;
            while (ret != 0) {
                ret /= 10;
                count ++;
            }
            ret = i;//ret = 123
            int sum = 0;
            while (ret != 0) {
                int n = 0;
                n = ret % 10;
                sum += Math.pow(n,count);//给n开count次方  pow返回值是double +=会自动类型转换
                ret /= 10;
            }
            if (sum == i) {
                System.out.println(i);
            }
        }
    }
    //判断一个数的二进制数中1的个数
    public static void GeShu() {
        Scanner scan = new Scanner(System.in);
        System.out.println("请输入十进制数：");
        int num = scan.nextInt();
        int count = 0;
        while (num != 0) {
            num = num & (num - 1);
            count++;
        }
        System.out.println(count);
    }
    //1到 100 的所有整数中出现多少个数字9
    public static void Nine() {
        int temp = 0;
        for (int i = 1; i <= 100 ; i++) {
            temp = i;
            while (temp != 0) {
                if (temp % 10 == 9) {
                    System.out.println(i);
                    break;
                }
                temp /= 10;
            }
        }
    }
    //计算1/1-1/2+1/3-1/4+1/5 …… + 1/99 - 1/100 的值
    public static void Num() {
        int flag = -1;
        double sum = 0.0;
        for (int i = 1; i <= 100; i++) {
            if (i%2 == 0) {
                sum = sum + flag*(1.0/i);
            }else {
                sum = sum + 1.0/i;
            }
        }
        System.out.println(sum);
    }
    //获取一个数二进制序列中所有的偶数位和奇数位， 分别输出二进制序列
    public static void X() {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        //0000 0010
        for (int i = 31; i >= 1 ; i-=2) {
            System.out.print(((n>>>i) & 1) + " " );
        }
        System.out.println();
        for (int i = 30; i >= 0 ; i-=2) {
            System.out.print(((n>>>i) & 1) + " " );
        }
    }



}
//程序调试  是一个程序员最基本的能力
//1.打断点  就是让程序运行到断点处停下来  本质上程序还在运行
//2.点击debug